<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"yang-da-xiong.gitee.io","root":"/","scheme":"Gemini","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}}};
  </script>

  <meta name="description" content="前言编译原理课程实验的实验课内容—构造词法分析程序。通过本次实验，希望对于编译原理中词法分析这个功能有更深和更好的理解。">
<meta property="og:type" content="article">
<meta property="og:title" content="编译原理--实验1 词法分析">
<meta property="og:url" content="https://yang-da-xiong.gitee.io/2022/01/12/%E7%BC%96%E8%AF%91%E5%8E%9F%E7%90%86--%E5%AE%9E%E9%AA%8C1%20%E8%AF%8D%E6%B3%95%E5%88%86%E6%9E%90/index.html">
<meta property="og:site_name" content="杨大熊的代码世界">
<meta property="og:description" content="前言编译原理课程实验的实验课内容—构造词法分析程序。通过本次实验，希望对于编译原理中词法分析这个功能有更深和更好的理解。">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://img-blog.csdnimg.cn/23f07cb549014f6ca4fea7856d57e2ee.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5p2o5aSn54aK55qE5Luj56CB5LiW55WM,size_20,color_FFFFFF,t_70,g_se,x_16">
<meta property="og:image" content="https://img-blog.csdnimg.cn/a930572d24764b5595b4fcc91575a775.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5p2o5aSn54aK55qE5Luj56CB5LiW55WM,size_15,color_FFFFFF,t_70,g_se,x_16">
<meta property="article:published_time" content="2022-01-12T02:20:57.289Z">
<meta property="article:modified_time" content="2022-01-12T03:07:34.680Z">
<meta property="article:author" content="杨大熊">
<meta property="article:tag" content="编译原理">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://img-blog.csdnimg.cn/23f07cb549014f6ca4fea7856d57e2ee.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5p2o5aSn54aK55qE5Luj56CB5LiW55WM,size_20,color_FFFFFF,t_70,g_se,x_16">

<link rel="canonical" href="https://yang-da-xiong.gitee.io/2022/01/12/%E7%BC%96%E8%AF%91%E5%8E%9F%E7%90%86--%E5%AE%9E%E9%AA%8C1%20%E8%AF%8D%E6%B3%95%E5%88%86%E6%9E%90/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>编译原理--实验1 词法分析 | 杨大熊的代码世界</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">杨大熊的代码世界</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
  </ul>
</nav>




</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://yang-da-xiong.gitee.io/2022/01/12/%E7%BC%96%E8%AF%91%E5%8E%9F%E7%90%86--%E5%AE%9E%E9%AA%8C1%20%E8%AF%8D%E6%B3%95%E5%88%86%E6%9E%90/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.png">
      <meta itemprop="name" content="杨大熊">
      <meta itemprop="description" content="各类IT知识分享">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="杨大熊的代码世界">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          编译原理--实验1 词法分析
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>
              

              <time title="创建时间：2022-01-12 10:20:57 / 修改时间：11:07:34" itemprop="dateCreated datePublished" datetime="2022-01-12T10:20:57+08:00">2022-01-12</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/%E7%BC%96%E8%AF%91%E5%8E%9F%E7%90%86/" itemprop="url" rel="index"><span itemprop="name">编译原理</span></a>
                </span>
            </span>

          
            <span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv" style="display: none;">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span id="busuanzi_value_page_pv"></span>
            </span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h1 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h1><p>编译原理课程实验的实验课内容—构造词法分析程序。通过本次实验，希望对于编译原理中词法分析这个功能有更深和更好的理解。</p>
<span id="more"></span>

<h1 id="1-1-实验目的"><a href="#1-1-实验目的" class="headerlink" title="1.1 实验目的"></a>1.1 实验目的</h1><p>（1）理解有穷自动机及其应用。 </p>
<p>（2）掌握 NFA 到 DFA 的等价变换方法、DFA 最小化的方法。 </p>
<p>（3）掌握设计、编码、调试词法分析程序的技术和方法。 </p>
<h1 id="1-2-实验任务"><a href="#1-2-实验任务" class="headerlink" title="1.2 实验任务"></a>1.2 实验任务</h1><p>编写一个程序对输入的源代码（源代码可以是C++, C, Java等）进行词法分析，并打印分析结果。</p>
<h1 id="1-3-实验内容"><a href="#1-3-实验内容" class="headerlink" title="1.3 实验内容"></a>1.3 实验内容</h1><h2 id="1-3-1-实验要求"><a href="#1-3-1-实验要求" class="headerlink" title="1.3.1 实验要求"></a>1.3.1 实验要求</h2><p>你的程序要能够查出源代码中可能包含的词法错误： 词法错误（错误类型 A）：（以输入 C–源代码为例）即出现 C–词法中未定义的字符 以及任何不符合 C–词法单元定义的字符； </p>
<h2 id="1-3-2-输入格式"><a href="#1-3-2-输入格式" class="headerlink" title="1.3.2 输入格式"></a>1.3.2 输入格式</h2><p>你的程序输入是一个包含 C++ 源代码的文本文件，程序需要能够接收一个输入文件名作为参数，以获得相应的输出结果。 </p>
<h2 id="1-3-3-输出格式"><a href="#1-3-3-输出格式" class="headerlink" title="1.3.3 输出格式"></a>1.3.3 输出格式</h2><p>要求通过标准输出打印程序的运行结果。对于那些包含词法错误的输入文件，只要输出相关的词法有误的信息即可。在这种情况下，注意不要输出任何与语法树有关的内容。 </p>
<p>要求输出的信息包括错误类型、出错的行号以及说明文字， 其格式为：</p>
<p>Error type [错误类型] at Line [行号]: [说明文字]. </p>
<p>说明文字的内容没有具体要求，但是错误类型和出错的行号一定要正确，因为这是判断 输出的错误提示信息是否正确的唯一标准。请严格遵守实验要求中给定的错误分类（即词法 错误为错误类型 A）.注意，输入文件中可能会包含一个或者多个词法错误（但输入文件的同一行中保证不出现多个错误），你的程序需要将这些错误全部报告出来，每一条错误提示信息在输出中单独占一行。 对于那些没有任何词法错误的输入文件，你的程序需要打印每一个词法单元的名称以及 与其对应的词素，无需打印行号。词法单元名与相应词素之间以一个冒号和一个空格隔开。 每一条词法单元的信息单独占一行。 </p>
<p>在很多程序设计语言中，下面的类别覆盖了大部分或所有的词法单元：</p>
<p> 1）关键字。一个关键字的类型就是该关键字本身。</p>
<p> 2）运算符。它可以表示单个运算符，也可以像表 1-1 中的 comparison 那样，表示一类 运算符。 </p>
<p> 3）标识符。一个表示所有标识符的词法单元。</p>
<p> 4）常量。一个或多个表示常量的词法单元，比如数字和字面值字符串。 5）界符。每一个标点符号有一个词法单元，比如左右括号、逗号和分号。 </p>
<p><img src="https://img-blog.csdnimg.cn/23f07cb549014f6ca4fea7856d57e2ee.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5p2o5aSn54aK55qE5Luj56CB5LiW55WM,size_20,color_FFFFFF,t_70,g_se,x_16" alt="在这里插入图片描述"></p>
<h2 id="1-3-4-样例"><a href="#1-3-4-样例" class="headerlink" title="1.3.4 样例"></a>1.3.4 样例</h2><p>样例1：</p>
<p>输入：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">int</span> i=<span class="number">1</span>;</span><br><span class="line">    <span class="keyword">int</span> j=~i;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>输出：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">Error type A at Line 4: Mysterious character &quot;~&quot;.</span><br></pre></td></tr></table></figure>



<p>样例2：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">inc</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">int</span> i;</span><br><span class="line">    i=<span class="number">100</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>输出：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">TYPE: <span class="keyword">int</span></span><br><span class="line">ID: inc</span><br><span class="line">LP: (</span><br><span class="line">RP: )</span><br><span class="line">LC: &#123;</span><br><span class="line">TYPE: <span class="keyword">int</span></span><br><span class="line">ID: i</span><br><span class="line">SEMI: ;</span><br><span class="line">ID: i</span><br><span class="line">RELOP: =</span><br><span class="line">INT: <span class="number">100</span></span><br><span class="line">SEMI: ;</span><br><span class="line">RC: &#125;</span><br></pre></td></tr></table></figure>

<h2 id="1-3-5-C–语言文法"><a href="#1-3-5-C–语言文法" class="headerlink" title="1.3.5 C–语言文法"></a>1.3.5 C–语言文法</h2><p>以下为C–文法中各个标识符或关键字等所对应的名称</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line">INT→ <span class="comment">/* A sequence of digits without spaces */</span></span><br><span class="line">FLOAT → <span class="comment">/* A real number consisting of digits and one decimal point. The decimal point must be surrounded by at least one digit */</span></span><br><span class="line">ID → <span class="comment">/* A character string consisting of 52 upper- or lower-case alphabetic, 10 numeric and one underscore characters. Besides, an identifier must not start with a digit */</span></span><br><span class="line">SEMI → ;</span><br><span class="line">COMMA → , </span><br><span class="line">ASSIGNOP→ = </span><br><span class="line">RELOP → &gt; | &lt; | &gt;= | &lt;= | == | !=</span><br><span class="line">PLUS → +</span><br><span class="line">MINUS → - </span><br><span class="line">STAR → * </span><br><span class="line">DIV → /</span><br><span class="line">AND → &amp;&amp;</span><br><span class="line">OR → ||</span><br><span class="line">DOT → . </span><br><span class="line">NOT → !</span><br><span class="line">TYPE →<span class="keyword">int</span> | <span class="keyword">float</span></span><br><span class="line">LP → (</span><br><span class="line">RP → )</span><br><span class="line">LB → [</span><br><span class="line">RB → ]</span><br><span class="line">LC → &#123;</span><br><span class="line">RC → &#125;</span><br><span class="line">STRUCT → struct</span><br><span class="line">RETURN → <span class="keyword">return</span></span><br><span class="line">IF → <span class="keyword">if</span></span><br><span class="line">ELSE → <span class="keyword">else</span></span><br><span class="line">WHILE → <span class="keyword">while</span></span><br></pre></td></tr></table></figure>



<h1 id="1-4-程序代码"><a href="#1-4-程序代码" class="headerlink" title="1.4 程序代码"></a>1.4 程序代码</h1><h2 id="1-4-1-程序流程图"><a href="#1-4-1-程序流程图" class="headerlink" title="1.4.1 程序流程图"></a>1.4.1 程序流程图</h2><p><img src="https://img-blog.csdnimg.cn/a930572d24764b5595b4fcc91575a775.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5p2o5aSn54aK55qE5Luj56CB5LiW55WM,size_15,color_FFFFFF,t_70,g_se,x_16" alt="在这里插入图片描述"></p>
<h2 id="1-4-2-程序源码"><a href="#1-4-2-程序源码" class="headerlink" title="1.4.2 程序源码"></a>1.4.2 程序源码</h2><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br><span class="line">228</span><br><span class="line">229</span><br><span class="line">230</span><br><span class="line">231</span><br><span class="line">232</span><br><span class="line">233</span><br><span class="line">234</span><br><span class="line">235</span><br><span class="line">236</span><br><span class="line">237</span><br><span class="line">238</span><br><span class="line">239</span><br><span class="line">240</span><br><span class="line">241</span><br><span class="line">242</span><br><span class="line">243</span><br><span class="line">244</span><br><span class="line">245</span><br><span class="line">246</span><br><span class="line">247</span><br><span class="line">248</span><br><span class="line">249</span><br><span class="line">250</span><br><span class="line">251</span><br><span class="line">252</span><br><span class="line">253</span><br><span class="line">254</span><br><span class="line">255</span><br><span class="line">256</span><br><span class="line">257</span><br><span class="line">258</span><br><span class="line">259</span><br><span class="line">260</span><br><span class="line">261</span><br><span class="line">262</span><br><span class="line">263</span><br><span class="line">264</span><br><span class="line">265</span><br><span class="line">266</span><br><span class="line">267</span><br><span class="line">268</span><br><span class="line">269</span><br><span class="line">270</span><br><span class="line">271</span><br><span class="line">272</span><br><span class="line">273</span><br><span class="line">274</span><br><span class="line">275</span><br><span class="line">276</span><br><span class="line">277</span><br><span class="line">278</span><br><span class="line">279</span><br><span class="line">280</span><br><span class="line">281</span><br><span class="line">282</span><br><span class="line">283</span><br><span class="line">284</span><br><span class="line">285</span><br><span class="line">286</span><br><span class="line">287</span><br><span class="line">288</span><br><span class="line">289</span><br><span class="line">290</span><br><span class="line">291</span><br><span class="line">292</span><br><span class="line">293</span><br><span class="line">294</span><br><span class="line">295</span><br><span class="line">296</span><br><span class="line">297</span><br><span class="line">298</span><br><span class="line">299</span><br><span class="line">300</span><br><span class="line">301</span><br><span class="line">302</span><br><span class="line">303</span><br><span class="line">304</span><br><span class="line">305</span><br><span class="line">306</span><br><span class="line">307</span><br><span class="line">308</span><br><span class="line">309</span><br><span class="line">310</span><br><span class="line">311</span><br><span class="line">312</span><br><span class="line">313</span><br><span class="line">314</span><br><span class="line">315</span><br><span class="line">316</span><br><span class="line">317</span><br><span class="line">318</span><br><span class="line">319</span><br><span class="line">320</span><br><span class="line">321</span><br><span class="line">322</span><br><span class="line">323</span><br><span class="line">324</span><br><span class="line">325</span><br><span class="line">326</span><br><span class="line">327</span><br><span class="line">328</span><br><span class="line">329</span><br><span class="line">330</span><br><span class="line">331</span><br><span class="line">332</span><br><span class="line">333</span><br><span class="line">334</span><br><span class="line">335</span><br><span class="line">336</span><br><span class="line">337</span><br><span class="line">338</span><br><span class="line">339</span><br><span class="line">340</span><br><span class="line">341</span><br><span class="line">342</span><br><span class="line">343</span><br><span class="line">344</span><br><span class="line">345</span><br><span class="line">346</span><br><span class="line">347</span><br><span class="line">348</span><br><span class="line">349</span><br><span class="line">350</span><br><span class="line">351</span><br><span class="line">352</span><br><span class="line">353</span><br><span class="line">354</span><br><span class="line">355</span><br><span class="line">356</span><br><span class="line">357</span><br><span class="line">358</span><br><span class="line">359</span><br><span class="line">360</span><br><span class="line">361</span><br><span class="line">362</span><br><span class="line">363</span><br><span class="line">364</span><br><span class="line">365</span><br><span class="line">366</span><br><span class="line">367</span><br><span class="line">368</span><br><span class="line">369</span><br><span class="line">370</span><br><span class="line">371</span><br><span class="line">372</span><br><span class="line">373</span><br><span class="line">374</span><br><span class="line">375</span><br><span class="line">376</span><br><span class="line">377</span><br><span class="line">378</span><br><span class="line">379</span><br><span class="line">380</span><br><span class="line">381</span><br><span class="line">382</span><br><span class="line">383</span><br><span class="line">384</span><br><span class="line">385</span><br><span class="line">386</span><br><span class="line">387</span><br><span class="line">388</span><br><span class="line">389</span><br><span class="line">390</span><br><span class="line">391</span><br><span class="line">392</span><br><span class="line">393</span><br><span class="line">394</span><br><span class="line">395</span><br><span class="line">396</span><br><span class="line">397</span><br><span class="line">398</span><br><span class="line">399</span><br><span class="line">400</span><br><span class="line">401</span><br><span class="line">402</span><br><span class="line">403</span><br><span class="line">404</span><br><span class="line">405</span><br><span class="line">406</span><br><span class="line">407</span><br><span class="line">408</span><br><span class="line">409</span><br><span class="line">410</span><br><span class="line">411</span><br><span class="line">412</span><br><span class="line">413</span><br><span class="line">414</span><br><span class="line">415</span><br><span class="line">416</span><br><span class="line">417</span><br><span class="line">418</span><br><span class="line">419</span><br><span class="line">420</span><br><span class="line">421</span><br><span class="line">422</span><br><span class="line">423</span><br><span class="line">424</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta"># <span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta"># <span class="meta-keyword">include</span><span class="meta-string">&lt;string&gt;</span></span></span><br><span class="line"><span class="meta"># <span class="meta-keyword">include</span><span class="meta-string">&lt;fstream&gt;</span></span></span><br><span class="line"><span class="meta"># <span class="meta-keyword">include</span><span class="meta-string">&lt;sstream&gt;</span><span class="comment">//流对象</span></span></span><br><span class="line"><span class="meta"># <span class="meta-keyword">include</span><span class="meta-string">&lt;vector&gt;</span></span></span><br><span class="line"><span class="meta"># <span class="meta-keyword">include</span><span class="meta-string">&lt;map&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">bool</span> <span class="title">isIdentifier</span><span class="params">(string s)</span></span>;<span class="comment">//标识符</span></span><br><span class="line"><span class="function"><span class="keyword">bool</span> <span class="title">isKeywords</span><span class="params">(string s)</span></span>;<span class="comment">//关键字</span></span><br><span class="line"><span class="function"><span class="keyword">bool</span> <span class="title">isDigit</span><span class="params">(string s)</span></span>;<span class="comment">//常数</span></span><br><span class="line"><span class="function"><span class="keyword">bool</span> <span class="title">isOperator</span><span class="params">(string s)</span></span>;<span class="comment">//运算符</span></span><br><span class="line"><span class="function"><span class="keyword">bool</span> <span class="title">isOperator</span><span class="params">(<span class="keyword">char</span> c)</span></span>;<span class="comment">//运算符</span></span><br><span class="line"><span class="function">string <span class="title">result</span><span class="params">(string s)</span></span>;<span class="comment">//根据传入的参数s产生对应的输出</span></span><br><span class="line">string miss = <span class="string">&quot;&quot;</span>;</span><br><span class="line"><span class="keyword">int</span> line_num = <span class="number">1</span>;</span><br><span class="line"><span class="keyword">int</span> flag = <span class="number">0</span>;  <span class="comment">//全局变量，判断输入的源代码是否有词法错误</span></span><br><span class="line"><span class="comment">//每次按行读，那么出错误就可以按行来看</span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="comment">//=====================测试函数是否正确=============================</span></span><br><span class="line">	<span class="comment">/*if(isIdentifier(&quot;_s2ias&quot;))</span></span><br><span class="line"><span class="comment">		cout &lt;&lt; &quot;是标识符&quot; &lt;&lt; endl;			//这样调试非常规范</span></span><br><span class="line"><span class="comment">	if (isKeywords(&quot;for&quot;))</span></span><br><span class="line"><span class="comment">		cout &lt;&lt; &quot;是关键字&quot; &lt;&lt; endl;</span></span><br><span class="line"><span class="comment">	if (isDigit(&quot;12a42&quot;))</span></span><br><span class="line"><span class="comment">		cout &lt;&lt; &quot;是数字&quot; &lt;&lt; endl;</span></span><br><span class="line"><span class="comment">	if (isOperator(&quot;&gt;&quot;))</span></span><br><span class="line"><span class="comment">		cout &lt;&lt; &quot;是运算符&quot; &lt;&lt; endl;</span></span><br><span class="line"><span class="comment">	result(&quot;?&quot;);*/</span></span><br><span class="line">	<span class="comment">//===================================================================</span></span><br><span class="line">	string file = (<span class="string">&quot;自定义测试样例3.txt&quot;</span>);</span><br><span class="line">	<span class="function">ifstream <span class="title">input</span><span class="params">(file)</span></span>;</span><br><span class="line">	<span class="comment">//输入文件流,注意编码，文本文件编码格式需和项目一直，否则乱码</span></span><br><span class="line">	string copy;</span><br><span class="line"></span><br><span class="line">	<span class="built_in">getline</span>(input, copy, <span class="string">&#x27;\0&#x27;</span>);		<span class="comment">//将文本的内容输出到屏幕上</span></span><br><span class="line">	cout &lt;&lt; copy &lt;&lt; endl;<span class="comment">//测试是否正确</span></span><br><span class="line"></span><br><span class="line">	<span class="comment">//此时input已经指到了文件尾，为了后面的读取，需要关闭再打开</span></span><br><span class="line">	input.<span class="built_in">close</span>();</span><br><span class="line">	input.<span class="built_in">open</span>(file);</span><br><span class="line"></span><br><span class="line">	<span class="comment">/*测试结果要求以原数据与结果对照的形式输出并保存在Result.txt中，</span></span><br><span class="line"><span class="comment">	同时要把结果输出到屏幕。</span></span><br><span class="line"><span class="comment">	*/</span></span><br><span class="line">	cout &lt;&lt; <span class="string">&quot;处理后结果:\n&quot;</span>;</span><br><span class="line">	string str;</span><br><span class="line">	string words;</span><br><span class="line">	<span class="keyword">while</span> (<span class="built_in">getline</span>(input, str)) <span class="comment">//读取文件每一次读取一行,遇到EOF结束</span></span><br><span class="line">	&#123;</span><br><span class="line">		<span class="comment">//从输入流中获取单词，需要用到输入流对象，即istringstream</span></span><br><span class="line">		<span class="function">istringstream <span class="title">strCin</span><span class="params">(str)</span></span>;</span><br><span class="line">		string s;</span><br><span class="line">		<span class="keyword">while</span> (strCin &gt;&gt; words)  <span class="comment">//这里的words可能是一大长串句子 , 按行处理</span></span><br><span class="line">		&#123;</span><br><span class="line">			<span class="comment">/*注意处理逗号，比如int a,b;这里有一个单词&quot;a,b;”,所以要处理一个字符串里面</span></span><br><span class="line"><span class="comment">			的各种运算符,但是这样会很麻烦，发现没有，用ide写代码写完一句输入分号时，ide</span></span><br><span class="line"><span class="comment">			会自动加入空格，这样就方便处理多了*/</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">			<span class="comment">//1.首先可以确定的是关键字肯定是单独作为一个单词的</span></span><br><span class="line">			<span class="keyword">if</span> (<span class="built_in">isKeywords</span>(words))</span><br><span class="line">			&#123;</span><br><span class="line">				s = <span class="built_in">result</span>(words);</span><br><span class="line">				<span class="keyword">continue</span>;</span><br><span class="line">			&#125;</span><br><span class="line"></span><br><span class="line">			<span class="comment">//2,对单词进行扫描，肯定是标识符，运算符，逗号分号,数字等等混合在一起的单词</span></span><br><span class="line">			vector&lt;<span class="keyword">int</span>&gt; index = &#123; <span class="number">0</span> &#125;;</span><br><span class="line">			vector&lt;string&gt; mulWords;<span class="comment">//将words分解为多个单词</span></span><br><span class="line">			<span class="keyword">if</span> (words.<span class="built_in">length</span>() == <span class="number">1</span>)</span><br><span class="line">			&#123;</span><br><span class="line">				string rel = <span class="built_in">result</span>(words);</span><br><span class="line">				<span class="keyword">continue</span>;</span><br><span class="line">			&#125;</span><br><span class="line">			<span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; words.<span class="built_in">length</span>(); i++)</span><br><span class="line">			&#123;</span><br><span class="line"></span><br><span class="line">				<span class="comment">//运算符有两位的，比如&quot;&lt;=&quot;,&quot;&gt;=&quot;,&quot;==&quot;,&quot;!=&quot;</span></span><br><span class="line">				<span class="keyword">if</span> ((i &lt; words.<span class="built_in">length</span>() - <span class="number">1</span>) &amp;&amp; <span class="built_in">isOperator</span>(words[i]) &amp;&amp; <span class="built_in">isOperator</span>(words[i + <span class="number">1</span>]))</span><br><span class="line">				&#123;</span><br><span class="line">					<span class="comment">//但是要注意只有以上四种两位运算符，比如+-,))就不是,但是))还是要输出),)</span></span><br><span class="line">					<span class="keyword">if</span> (<span class="built_in">string</span>(words.<span class="built_in">begin</span>() + i, words.<span class="built_in">begin</span>() + i + <span class="number">2</span>) == <span class="string">&quot;&lt;=&quot;</span> ||</span><br><span class="line">						<span class="built_in">string</span>(words.<span class="built_in">begin</span>() + i, words.<span class="built_in">begin</span>() + i + <span class="number">2</span>) == <span class="string">&quot;&gt;=&quot;</span> ||</span><br><span class="line">						<span class="built_in">string</span>(words.<span class="built_in">begin</span>() + i, words.<span class="built_in">begin</span>() + i + <span class="number">2</span>) == <span class="string">&quot;==&quot;</span> ||</span><br><span class="line">						<span class="built_in">string</span>(words.<span class="built_in">begin</span>() + i, words.<span class="built_in">begin</span>() + i + <span class="number">2</span>) == <span class="string">&quot;!=&quot;</span>)</span><br><span class="line">					&#123;</span><br><span class="line">						index.<span class="built_in">push_back</span>(i);</span><br><span class="line">						index.<span class="built_in">push_back</span>(i + <span class="number">2</span>);</span><br><span class="line">						i++;</span><br><span class="line">					&#125;</span><br><span class="line">					<span class="keyword">else</span> <span class="keyword">if</span> (<span class="built_in">isOperator</span>(words[i]))</span><br><span class="line">					&#123;</span><br><span class="line">						<span class="keyword">if</span> (<span class="built_in">find</span>(index.<span class="built_in">begin</span>(), index.<span class="built_in">end</span>(), i) == index.<span class="built_in">end</span>())   <span class="comment">//查找失败，index中没有i这个数，插入</span></span><br><span class="line">							index.<span class="built_in">push_back</span>(i);</span><br><span class="line">						<span class="keyword">if</span> (<span class="built_in">find</span>(index.<span class="built_in">begin</span>(), index.<span class="built_in">end</span>(), i + <span class="number">1</span>) == index.<span class="built_in">end</span>())   <span class="comment">//查找失败，index中没有i+1这个数，插入</span></span><br><span class="line">							index.<span class="built_in">push_back</span>(i + <span class="number">1</span>);</span><br><span class="line"></span><br><span class="line">					&#125;</span><br><span class="line">				&#125;</span><br><span class="line">				<span class="comment">//逗号，运算符作为分隔 ，也包括最后一个字符是不是运算符</span></span><br><span class="line">				<span class="keyword">else</span> <span class="keyword">if</span> (<span class="built_in">isOperator</span>(words[i]))</span><br><span class="line">				&#123;</span><br><span class="line">					<span class="keyword">if</span> (<span class="built_in">find</span>(index.<span class="built_in">begin</span>(), index.<span class="built_in">end</span>(), i) == index.<span class="built_in">end</span>())</span><br><span class="line">						<span class="comment">//比如遇到&quot;a,b&quot;这里下标0和1将a分开，1到2将逗号分开，2到3将b分开</span></span><br><span class="line">						index.<span class="built_in">push_back</span>(i);</span><br><span class="line">					<span class="keyword">if</span> (<span class="built_in">find</span>(index.<span class="built_in">begin</span>(), index.<span class="built_in">end</span>(), i + <span class="number">1</span>) == index.<span class="built_in">end</span>())</span><br><span class="line">						index.<span class="built_in">push_back</span>(i + <span class="number">1</span>);</span><br><span class="line"></span><br><span class="line">					<span class="comment">//如果是a&lt;=b这样的呢？一样，先0和1将a分开，1和2将&lt;分开，2和3将=分开</span></span><br><span class="line">					<span class="comment">//3和4将b分开，然后后面分隔单词时，注意如果相邻都是运算符，则忽略，比如</span></span><br><span class="line">					<span class="comment">//后面判断到1和2，2和3都是运算符，则忽略2</span></span><br><span class="line"></span><br><span class="line">				&#125;</span><br><span class="line"></span><br><span class="line">			&#125;</span><br><span class="line">			<span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; index.<span class="built_in">size</span>() - <span class="number">1</span>; i++)</span><br><span class="line">			&#123;</span><br><span class="line">				string rel;</span><br><span class="line">				<span class="comment">//比如遇到&quot;&lt;=&quot;，需要提取”&lt;=“</span></span><br><span class="line">				<span class="comment">/*if (isOperator(words[index[i]]) &amp;&amp; isOperator(words[index[i + 1]]))</span></span><br><span class="line"><span class="comment">				&#123;</span></span><br><span class="line"><span class="comment">					rel = result(string(words.begin() + index[i], words.begin() + index[i + 2]));</span></span><br><span class="line"><span class="comment">					i++;</span></span><br><span class="line"><span class="comment">				&#125;</span></span><br><span class="line"><span class="comment">				else*/</span></span><br><span class="line">				rel = <span class="built_in">result</span>(<span class="built_in">string</span>(words.<span class="built_in">begin</span>() + index[i], words.<span class="built_in">begin</span>() + index[i + <span class="number">1</span>])); <span class="comment">//每次传入前后的两个参数指令</span></span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">		miss = <span class="string">&quot;&quot;</span>;  <span class="comment">//换行之后要清空miss</span></span><br><span class="line">		line_num++;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">if</span> (flag == <span class="number">0</span>)</span><br><span class="line">	&#123;</span><br><span class="line">		input.<span class="built_in">close</span>();</span><br><span class="line">		input.<span class="built_in">open</span>(file);</span><br><span class="line">		str = <span class="string">&quot;&quot;</span>;</span><br><span class="line">		words = <span class="string">&quot;&quot;</span>;</span><br><span class="line">		<span class="keyword">while</span> (<span class="built_in">getline</span>(input, str)) <span class="comment">//读取文件每一次读取一行,遇到EOF结束</span></span><br><span class="line">		&#123;</span><br><span class="line">			<span class="comment">//从输入流中获取单词，需要用到输入流对象，即istringstream</span></span><br><span class="line">			<span class="function">istringstream <span class="title">strCin</span><span class="params">(str)</span></span>;</span><br><span class="line">			string s;</span><br><span class="line">			<span class="keyword">while</span> (strCin &gt;&gt; words)  <span class="comment">//这里的words可能是一大长串句子 , 按行处理</span></span><br><span class="line">			&#123;</span><br><span class="line">				<span class="comment">/*注意处理逗号，比如int a,b;这里有一个单词&quot;a,b;”,所以要处理一个字符串里面</span></span><br><span class="line"><span class="comment">				的各种运算符,但是这样会很麻烦，发现没有，用ide写代码写完一句输入分号时，ide</span></span><br><span class="line"><span class="comment">				会自动加入空格，这样就方便处理多了*/</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">				<span class="comment">//1.首先可以确定的是关键字肯定是单独作为一个单词的</span></span><br><span class="line">				<span class="keyword">if</span> (<span class="built_in">isKeywords</span>(words))</span><br><span class="line">				&#123;</span><br><span class="line">					s = <span class="built_in">result</span>(words);</span><br><span class="line">					cout &lt;&lt; s &lt;&lt; endl;</span><br><span class="line">					<span class="keyword">continue</span>;</span><br><span class="line">				&#125;</span><br><span class="line"></span><br><span class="line">				<span class="comment">//2,对单词进行扫描，肯定是标识符，运算符，逗号分号,数字等等混合在一起的单词</span></span><br><span class="line">				vector&lt;<span class="keyword">int</span>&gt; index = &#123; <span class="number">0</span> &#125;;</span><br><span class="line">				vector&lt;string&gt; mulWords;<span class="comment">//将words分解为多个单词</span></span><br><span class="line">				<span class="keyword">if</span> (words.<span class="built_in">length</span>() == <span class="number">1</span>)</span><br><span class="line">				&#123;</span><br><span class="line">					string rel = <span class="built_in">result</span>(words);</span><br><span class="line">					cout &lt;&lt; rel &lt;&lt; endl;</span><br><span class="line">					<span class="keyword">continue</span>;</span><br><span class="line">				&#125;</span><br><span class="line">				<span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; words.<span class="built_in">length</span>(); i++)</span><br><span class="line">				&#123;</span><br><span class="line"></span><br><span class="line">					<span class="comment">//运算符有两位的，比如&quot;&lt;=&quot;,&quot;&gt;=&quot;,&quot;==&quot;,&quot;!=&quot;</span></span><br><span class="line">					<span class="keyword">if</span> ((i &lt; words.<span class="built_in">length</span>() - <span class="number">1</span>) &amp;&amp; <span class="built_in">isOperator</span>(words[i]) &amp;&amp; <span class="built_in">isOperator</span>(words[i + <span class="number">1</span>]))</span><br><span class="line">					&#123;</span><br><span class="line">						<span class="comment">//但是要注意只有以上四种两位运算符，比如+-,))就不是,但是))还是要输出),)</span></span><br><span class="line">						<span class="keyword">if</span> (<span class="built_in">string</span>(words.<span class="built_in">begin</span>() + i, words.<span class="built_in">begin</span>() + i + <span class="number">2</span>) == <span class="string">&quot;&lt;=&quot;</span> ||</span><br><span class="line">							<span class="built_in">string</span>(words.<span class="built_in">begin</span>() + i, words.<span class="built_in">begin</span>() + i + <span class="number">2</span>) == <span class="string">&quot;&gt;=&quot;</span> ||</span><br><span class="line">							<span class="built_in">string</span>(words.<span class="built_in">begin</span>() + i, words.<span class="built_in">begin</span>() + i + <span class="number">2</span>) == <span class="string">&quot;==&quot;</span> ||</span><br><span class="line">							<span class="built_in">string</span>(words.<span class="built_in">begin</span>() + i, words.<span class="built_in">begin</span>() + i + <span class="number">2</span>) == <span class="string">&quot;!=&quot;</span>)</span><br><span class="line">						&#123;</span><br><span class="line">							index.<span class="built_in">push_back</span>(i);</span><br><span class="line">							index.<span class="built_in">push_back</span>(i + <span class="number">2</span>);</span><br><span class="line">							i++;</span><br><span class="line">						&#125;</span><br><span class="line">						<span class="keyword">else</span> <span class="keyword">if</span> (<span class="built_in">isOperator</span>(words[i]))</span><br><span class="line">						&#123;</span><br><span class="line">							<span class="keyword">if</span> (<span class="built_in">find</span>(index.<span class="built_in">begin</span>(), index.<span class="built_in">end</span>(), i) == index.<span class="built_in">end</span>())   <span class="comment">//查找失败，index中没有i这个数，插入</span></span><br><span class="line">								index.<span class="built_in">push_back</span>(i);</span><br><span class="line">							<span class="keyword">if</span> (<span class="built_in">find</span>(index.<span class="built_in">begin</span>(), index.<span class="built_in">end</span>(), i + <span class="number">1</span>) == index.<span class="built_in">end</span>())   <span class="comment">//查找失败，index中没有i+1这个数，插入</span></span><br><span class="line">								index.<span class="built_in">push_back</span>(i + <span class="number">1</span>);</span><br><span class="line"></span><br><span class="line">						&#125;</span><br><span class="line">					&#125;</span><br><span class="line">					<span class="comment">//逗号，运算符作为分隔 ，也包括最后一个字符是不是运算符</span></span><br><span class="line">					<span class="keyword">else</span> <span class="keyword">if</span> (<span class="built_in">isOperator</span>(words[i]))</span><br><span class="line">					&#123;</span><br><span class="line">						<span class="keyword">if</span> (<span class="built_in">find</span>(index.<span class="built_in">begin</span>(), index.<span class="built_in">end</span>(), i) == index.<span class="built_in">end</span>())</span><br><span class="line">							<span class="comment">//比如遇到&quot;a,b&quot;这里下标0和1将a分开，1到2将逗号分开，2到3将b分开</span></span><br><span class="line">							index.<span class="built_in">push_back</span>(i);</span><br><span class="line">						<span class="keyword">if</span> (<span class="built_in">find</span>(index.<span class="built_in">begin</span>(), index.<span class="built_in">end</span>(), i + <span class="number">1</span>) == index.<span class="built_in">end</span>())</span><br><span class="line">							index.<span class="built_in">push_back</span>(i + <span class="number">1</span>);</span><br><span class="line"></span><br><span class="line">						<span class="comment">//如果是a&lt;=b这样的呢？一样，先0和1将a分开，1和2将&lt;分开，2和3将=分开</span></span><br><span class="line">						<span class="comment">//3和4将b分开，然后后面分隔单词时，注意如果相邻都是运算符，则忽略，比如</span></span><br><span class="line">						<span class="comment">//后面判断到1和2，2和3都是运算符，则忽略2</span></span><br><span class="line"></span><br><span class="line">					&#125;</span><br><span class="line"></span><br><span class="line">				&#125;</span><br><span class="line">				<span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; index.<span class="built_in">size</span>() - <span class="number">1</span>; i++)</span><br><span class="line">				&#123;</span><br><span class="line">					string rel;</span><br><span class="line">					<span class="comment">//比如遇到&quot;&lt;=&quot;，需要提取”&lt;=“</span></span><br><span class="line">					<span class="comment">/*if (isOperator(words[index[i]]) &amp;&amp; isOperator(words[index[i + 1]]))</span></span><br><span class="line"><span class="comment">					&#123;</span></span><br><span class="line"><span class="comment">						rel = result(string(words.begin() + index[i], words.begin() + index[i + 2]));</span></span><br><span class="line"><span class="comment">						i++;</span></span><br><span class="line"><span class="comment">					&#125;</span></span><br><span class="line"><span class="comment">					else*/</span></span><br><span class="line">					rel = <span class="built_in">result</span>(<span class="built_in">string</span>(words.<span class="built_in">begin</span>() + index[i], words.<span class="built_in">begin</span>() + index[i + <span class="number">1</span>])); <span class="comment">//每次传入前后的两个参数指令</span></span><br><span class="line">					cout &lt;&lt; rel &lt;&lt; endl;</span><br><span class="line">				&#125;</span><br><span class="line">				<span class="keyword">if</span> (index.<span class="built_in">size</span>() &lt;= <span class="number">1</span>)</span><br><span class="line">				&#123;</span><br><span class="line">					string rel = <span class="built_in">result</span>(words);</span><br><span class="line">					cout &lt;&lt; rel &lt;&lt; endl;</span><br><span class="line">				&#125;</span><br><span class="line">			&#125;</span><br><span class="line">			miss = <span class="string">&quot;&quot;</span>;  <span class="comment">//换行之后要清空miss</span></span><br><span class="line">			line_num++;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	input.<span class="built_in">close</span>();</span><br><span class="line">	<span class="built_in">system</span>(<span class="string">&quot;pause&quot;</span>);</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">bool</span> <span class="title">isIdentifier</span><span class="params">(string s)</span><span class="comment">//标识符,试验要求:ID=letter(letter|digit)*</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="keyword">if</span> (!<span class="built_in">isKeywords</span>(s))<span class="comment">//标识符不能是关键字</span></span><br><span class="line">	&#123;</span><br><span class="line">		<span class="keyword">if</span> ((s[<span class="number">0</span>] &gt;= <span class="string">&#x27;a&#x27;</span>&amp;&amp;s[<span class="number">0</span>] &lt;= <span class="string">&#x27;z&#x27;</span>) || (s[<span class="number">0</span>] &gt;= <span class="string">&#x27;A&#x27;</span>&amp;&amp;s[<span class="number">0</span>] &lt;= <span class="string">&#x27;Z&#x27;</span>))<span class="comment">//是字母</span></span><br><span class="line">		&#123;</span><br><span class="line">			<span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i &lt; s.<span class="built_in">length</span>(); i++)</span><br><span class="line">			&#123;</span><br><span class="line">				<span class="keyword">if</span> ((s[i] &gt;= <span class="string">&#x27;a&#x27;</span>&amp;&amp;s[i] &lt;= <span class="string">&#x27;z&#x27;</span>) || (s[i] &gt;= <span class="string">&#x27;A&#x27;</span>&amp;&amp;s[i] &lt;= <span class="string">&#x27;Z&#x27;</span>)</span><br><span class="line">					|| (s[i] &gt;= <span class="string">&#x27;0&#x27;</span>&amp;&amp;s[i] &lt;= <span class="string">&#x27;9&#x27;</span>))</span><br><span class="line">					<span class="keyword">continue</span>;</span><br><span class="line">				<span class="keyword">else</span></span><br><span class="line">				&#123;</span><br><span class="line">					miss = s[i];</span><br><span class="line">					<span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">				&#125;</span><br><span class="line">			&#125;</span><br><span class="line">			miss = <span class="string">&quot;&quot;</span>;</span><br><span class="line">			<span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">else</span></span><br><span class="line">		&#123;</span><br><span class="line">			miss = s[<span class="number">0</span>];</span><br><span class="line">			<span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">bool</span> <span class="title">isKeywords</span><span class="params">(string s)</span><span class="comment">//判断是不是关键字</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="keyword">static</span> vector&lt;string&gt; keyVec = &#123; <span class="string">&quot;main&quot;</span>, <span class="string">&quot;int&quot;</span>, <span class="string">&quot;float&quot;</span>, <span class="string">&quot;double&quot;</span>, <span class="string">&quot;char&quot;</span>,</span><br><span class="line">		<span class="string">&quot;if&quot;</span>, <span class="string">&quot;then&quot;</span>,<span class="string">&quot;else&quot;</span>, <span class="string">&quot;switch&quot;</span>, <span class="string">&quot;case&quot;</span>, <span class="string">&quot;break&quot;</span>, <span class="string">&quot;continue&quot;</span>, <span class="string">&quot;while&quot;</span>,</span><br><span class="line">		<span class="string">&quot;do&quot;</span>, <span class="string">&quot;for&quot;</span>,<span class="string">&quot;struct&quot;</span>,<span class="string">&quot;.&quot;</span> &#125;;</span><br><span class="line">	vector&lt;string&gt;::iterator result = <span class="built_in">find</span>(keyVec.<span class="built_in">begin</span>(), keyVec.<span class="built_in">end</span>(), s);</span><br><span class="line">	<span class="keyword">if</span> (result != keyVec.<span class="built_in">end</span>())</span><br><span class="line">		<span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">	<span class="keyword">else</span> <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">bool</span> <span class="title">isDigit</span><span class="params">(string s)</span><span class="comment">//整型数字,NUM=digit digit*</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="keyword">if</span> (s[<span class="number">0</span>] &gt;= <span class="string">&#x27;0&#x27;</span>&amp;&amp;s[<span class="number">0</span>] &lt;= <span class="string">&#x27;9&#x27;</span>)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i &lt; s.<span class="built_in">length</span>(); i++)</span><br><span class="line">		&#123;</span><br><span class="line">			<span class="keyword">if</span> (s[i] &gt;= <span class="string">&#x27;0&#x27;</span>&amp;&amp;s[i] &lt;= <span class="string">&#x27;9&#x27;</span>)</span><br><span class="line">				<span class="keyword">continue</span>;</span><br><span class="line">			<span class="keyword">else</span></span><br><span class="line">			&#123;</span><br><span class="line">				miss = s[i];</span><br><span class="line">				<span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">		miss = <span class="string">&quot;&quot;</span>;</span><br><span class="line">		<span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">bool</span> <span class="title">isFloat</span><span class="params">(string s)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="keyword">int</span> flag3 = <span class="number">0</span>;</span><br><span class="line">	<span class="keyword">if</span> (s[<span class="number">0</span>] &gt;= <span class="string">&#x27;0&#x27;</span>&amp;&amp;s[<span class="number">0</span>] &lt;= <span class="string">&#x27;9&#x27;</span>)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i &lt; s.<span class="built_in">length</span>(); i++)</span><br><span class="line">		&#123;</span><br><span class="line">			<span class="keyword">if</span> (s[i] &gt;= <span class="string">&#x27;0&#x27;</span>&amp;&amp;s[i] &lt;= <span class="string">&#x27;9&#x27;</span>)</span><br><span class="line">				<span class="keyword">continue</span>;</span><br><span class="line">			<span class="keyword">else</span> <span class="keyword">if</span> (s[i] == <span class="string">&#x27;.&#x27;</span>)</span><br><span class="line">			&#123;</span><br><span class="line">				flag3 = <span class="number">1</span>;</span><br><span class="line">				<span class="keyword">continue</span>;</span><br><span class="line">			&#125;</span><br><span class="line">			<span class="keyword">else</span> <span class="keyword">if</span> (s[i] == <span class="string">&#x27;e&#x27;</span>)</span><br><span class="line">			&#123;</span><br><span class="line">				<span class="keyword">continue</span>;</span><br><span class="line">			&#125;</span><br><span class="line">			<span class="keyword">else</span></span><br><span class="line">			&#123;</span><br><span class="line">				miss = s[i];</span><br><span class="line">				<span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">if</span> (flag3 == <span class="number">0</span>)</span><br><span class="line">		&#123;</span><br><span class="line">			<span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">		&#125;</span><br><span class="line">		miss = <span class="string">&quot;&quot;</span>;</span><br><span class="line">		<span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">bool</span> <span class="title">isOperator</span><span class="params">(string s)</span> <span class="comment">//运算符 </span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="keyword">static</span> vector&lt;string&gt; opeVec = &#123; <span class="string">&quot;=&quot;</span>,<span class="string">&quot;+&quot;</span>,<span class="string">&quot;-&quot;</span>,<span class="string">&quot;*&quot;</span>,<span class="string">&quot;/&quot;</span>,<span class="string">&quot;&lt;&quot;</span>,<span class="string">&quot;&lt;=&quot;</span>,<span class="string">&quot;==&quot;</span>,<span class="string">&quot;!=&quot;</span>,</span><br><span class="line">		<span class="string">&quot;&gt;&quot;</span>,<span class="string">&quot;&gt;=&quot;</span>,<span class="string">&quot;;&quot;</span>,<span class="string">&quot;(&quot;</span>,<span class="string">&quot;)&quot;</span>,<span class="string">&quot;:&quot;</span>,<span class="string">&quot;,&quot;</span>,<span class="string">&quot;&#123;&quot;</span>,<span class="string">&quot;&#125;&quot;</span>,<span class="string">&quot;&amp;&amp;&quot;</span>,<span class="string">&quot;||&quot;</span>,<span class="string">&quot;!&quot;</span> &#125;;</span><br><span class="line">	vector&lt;string&gt;::iterator result = <span class="built_in">find</span>(opeVec.<span class="built_in">begin</span>(), opeVec.<span class="built_in">end</span>(), s);</span><br><span class="line">	<span class="keyword">if</span> (result != opeVec.<span class="built_in">end</span>())</span><br><span class="line">	&#123;</span><br><span class="line">		miss = <span class="string">&quot;&quot;</span>;</span><br><span class="line">		<span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">else</span></span><br><span class="line">	&#123;</span><br><span class="line">		miss = s;</span><br><span class="line">		<span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">bool</span> <span class="title">isOperator</span><span class="params">(<span class="keyword">char</span> c)</span><span class="comment">//运算符</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="keyword">static</span> vector&lt;<span class="keyword">char</span>&gt; opeVec = &#123; <span class="string">&#x27;=&#x27;</span>,<span class="string">&#x27;+&#x27;</span>,<span class="string">&#x27;-&#x27;</span>,<span class="string">&#x27;*&#x27;</span>,<span class="string">&#x27;/&#x27;</span>,<span class="string">&#x27;&lt;&#x27;</span>,</span><br><span class="line">		<span class="comment">//&quot;&lt;=&quot;,&quot;==&quot;,&quot;!=&quot;,</span></span><br><span class="line">		<span class="string">&#x27;&gt;&#x27;</span>,</span><br><span class="line">		<span class="comment">//&quot;&gt;=&quot;,</span></span><br><span class="line">		<span class="string">&#x27;;&#x27;</span>,<span class="string">&#x27;(&#x27;</span>,<span class="string">&#x27;)&#x27;</span>,<span class="string">&#x27;?&#x27;</span>,<span class="string">&#x27;:&#x27;</span>,<span class="string">&#x27;,&#x27;</span>,<span class="string">&#x27;&#123;&#x27;</span>,<span class="string">&#x27;&#125;&#x27;</span>,<span class="string">&#x27;!&#x27;</span> &#125;;</span><br><span class="line">	vector&lt;<span class="keyword">char</span>&gt;::iterator result = <span class="built_in">find</span>(opeVec.<span class="built_in">begin</span>(), opeVec.<span class="built_in">end</span>(), c);</span><br><span class="line">	<span class="keyword">if</span> (result != opeVec.<span class="built_in">end</span>())</span><br><span class="line">		<span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">	<span class="keyword">else</span></span><br><span class="line">	&#123;</span><br><span class="line">		miss = c;</span><br><span class="line">		<span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function">string <span class="title">result</span><span class="params">(string s)</span><span class="comment">//根据传入的参数s产生对应的输出</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="comment">//种别码</span></span><br><span class="line">	<span class="comment">//1.标识符</span></span><br><span class="line">	<span class="keyword">if</span> (<span class="built_in">isIdentifier</span>(s))</span><br><span class="line">		<span class="keyword">return</span> <span class="string">&quot;ID: &quot;</span> + s;</span><br><span class="line"></span><br><span class="line">	<span class="comment">//2.关键字</span></span><br><span class="line">	<span class="keyword">static</span> map&lt;string, string&gt; keyMap;</span><br><span class="line">	keyMap[<span class="string">&quot;int&quot;</span>] = <span class="string">&quot;TYPE: &quot;</span>;</span><br><span class="line">	keyMap[<span class="string">&quot;main&quot;</span>] = <span class="string">&quot;MAIN: &quot;</span>;</span><br><span class="line">	keyMap[<span class="string">&quot;float&quot;</span>] = <span class="string">&quot;TYPE: &quot;</span>;</span><br><span class="line">	keyMap[<span class="string">&quot;if&quot;</span>] = <span class="string">&quot;IF: &quot;</span>;</span><br><span class="line">	keyMap[<span class="string">&quot;struct&quot;</span>] = <span class="string">&quot;STRUCT: &quot;</span>;</span><br><span class="line">	keyMap[<span class="string">&quot;return&quot;</span>] = <span class="string">&quot;RETURN: &quot;</span>;</span><br><span class="line">	keyMap[<span class="string">&quot;else&quot;</span>] = <span class="string">&quot;ELSE: &quot;</span>;</span><br><span class="line">	keyMap[<span class="string">&quot;while&quot;</span>] = <span class="string">&quot;WHILE: &quot;</span>;</span><br><span class="line">	keyMap[<span class="string">&quot;.&quot;</span>] = <span class="string">&quot;DOT: &quot;</span>;</span><br><span class="line">	<span class="comment">//这里后续把关键字补全</span></span><br><span class="line">	<span class="keyword">if</span> (<span class="built_in">isKeywords</span>(s))</span><br><span class="line">		<span class="keyword">return</span> keyMap[s] + s;</span><br><span class="line">	<span class="comment">//3.浮点型常量</span></span><br><span class="line">	<span class="keyword">if</span> (<span class="built_in">isFloat</span>(s))</span><br><span class="line">		<span class="keyword">return</span> <span class="string">&quot;FLOAT: &quot;</span> + s;</span><br><span class="line">	<span class="comment">//3.整型常量</span></span><br><span class="line">	<span class="keyword">else</span> <span class="keyword">if</span> (<span class="built_in">isDigit</span>(s))</span><br><span class="line">		<span class="keyword">return</span> <span class="string">&quot;INT: &quot;</span> + s;</span><br><span class="line"></span><br><span class="line">	<span class="comment">//4.运算符</span></span><br><span class="line">	<span class="keyword">static</span> map&lt;string, string&gt; opeMap;</span><br><span class="line">	opeMap[<span class="string">&quot;=&quot;</span>] = <span class="string">&quot;RELOP: =&quot;</span>;</span><br><span class="line">	opeMap[<span class="string">&quot;&lt;&quot;</span>] = <span class="string">&quot;RELOP: &lt;&quot;</span>;</span><br><span class="line">	opeMap[<span class="string">&quot;&lt;=&quot;</span>] = <span class="string">&quot;RELOP: &lt;=&quot;</span>;</span><br><span class="line">	opeMap[<span class="string">&quot;==&quot;</span>] = <span class="string">&quot;RELOP: ==&quot;</span>;</span><br><span class="line">	opeMap[<span class="string">&quot;!=&quot;</span>] = <span class="string">&quot;RELOP: !=&quot;</span>;</span><br><span class="line">	opeMap[<span class="string">&quot;&gt;&quot;</span>] = <span class="string">&quot;RELOP: &gt;&quot;</span>;</span><br><span class="line">	opeMap[<span class="string">&quot;&gt;=&quot;</span>] = <span class="string">&quot;RELOP: &gt;=&quot;</span>;</span><br><span class="line">	opeMap[<span class="string">&quot;;&quot;</span>] = <span class="string">&quot;SEMI: ;&quot;</span>;</span><br><span class="line">	opeMap[<span class="string">&quot;+&quot;</span>] = <span class="string">&quot;PLUS: +&quot;</span>;</span><br><span class="line">	opeMap[<span class="string">&quot;(&quot;</span>] = <span class="string">&quot;LP: (&quot;</span>;</span><br><span class="line">	opeMap[<span class="string">&quot;-&quot;</span>] = <span class="string">&quot;MINUS: -&quot;</span>;</span><br><span class="line">	opeMap[<span class="string">&quot;)&quot;</span>] = <span class="string">&quot;RP: )&quot;</span>;</span><br><span class="line">	opeMap[<span class="string">&quot;*&quot;</span>] = <span class="string">&quot;STAR: *&quot;</span>;</span><br><span class="line">	opeMap[<span class="string">&quot;/&quot;</span>] = <span class="string">&quot;DIV: /&quot;</span>;</span><br><span class="line">	opeMap[<span class="string">&quot;,&quot;</span>] = <span class="string">&quot;COMMA: ,&quot;</span>;</span><br><span class="line">	opeMap[<span class="string">&quot;&#123;&quot;</span>] = <span class="string">&quot;LC: &#123;&quot;</span>;</span><br><span class="line">	opeMap[<span class="string">&quot;&#125;&quot;</span>] = <span class="string">&quot;RC: &#125;&quot;</span>;</span><br><span class="line">	opeMap[<span class="string">&quot;[&quot;</span>] = <span class="string">&quot;LB: [&quot;</span>;</span><br><span class="line">	opeMap[<span class="string">&quot;]&quot;</span>] = <span class="string">&quot;RB: ]&quot;</span>;</span><br><span class="line">	opeMap[<span class="string">&quot;!&quot;</span>] = <span class="string">&quot;NOT: !&quot;</span>;</span><br><span class="line">	opeMap[<span class="string">&quot;&amp;&amp;&quot;</span>] = <span class="string">&quot;AND: &amp;&amp;&quot;</span>;</span><br><span class="line">	opeMap[<span class="string">&quot;||&quot;</span>] = <span class="string">&quot;OR: ||&quot;</span>;</span><br><span class="line">	<span class="keyword">if</span> (<span class="built_in">isOperator</span>(s))</span><br><span class="line">		<span class="keyword">return</span> opeMap[s];</span><br><span class="line">	flag = <span class="number">1</span>;</span><br><span class="line">	string ans = <span class="string">&quot;Error type A at Line &quot;</span> + <span class="built_in">to_string</span>(line_num) + <span class="string">&quot; Mysterious character &#x27;&quot;</span> + miss + <span class="string">&quot;&#x27;&quot;</span>;</span><br><span class="line">	cout &lt;&lt; ans &lt;&lt; endl;</span><br><span class="line">	<span class="keyword">return</span> <span class="string">&quot;Error type A at Line &quot;</span> + <span class="built_in">to_string</span>(line_num) + <span class="string">&quot; Mysterious character &#x27;&quot;</span> + miss + <span class="string">&quot;&#x27;&quot;</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h1 id="1-5-总结"><a href="#1-5-总结" class="headerlink" title="1.5 总结"></a>1.5 总结</h1><p>这次实验让我掌握了一个小型词法分析程序完整的构造过程，从查阅各种资料开始，到慢慢的理解别人的代码和思想，再到自己动手去编写一个词法分析程序。这样的一个过程既锻炼了自己的学习能力，也锻炼了自己的思维和编程能力，帮助我更好的学习编译原理这门课程，令我感到受益匪浅。 </p>
<p>参考文献：<a target="_blank" rel="noopener" href="https://blog.csdn.net/qq_33001647/article/details/79680578">(38条消息) 编译原理实验一:词法分析_那又怎样的博客-CSDN博客_编译原理实验一 词法分析</a> </p>

    </div>

    
    
    
        

<div>
<ul class="post-copyright">
  <li class="post-copyright-author">
    <strong>本文作者： </strong>杨大熊
  </li>
  <li class="post-copyright-link">
    <strong>本文链接：</strong>
    <a href="https://yang-da-xiong.gitee.io/2022/01/12/%E7%BC%96%E8%AF%91%E5%8E%9F%E7%90%86--%E5%AE%9E%E9%AA%8C1%20%E8%AF%8D%E6%B3%95%E5%88%86%E6%9E%90/" title="编译原理--实验1 词法分析">https://yang-da-xiong.gitee.io/2022/01/12/编译原理--实验1 词法分析/</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" rel="noopener" target="_blank"><i class="fab fa-fw fa-creative-commons"></i>BY-NC-SA</a> 许可协议。转载请注明出处！
  </li>
</ul>
</div>


      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/%E7%BC%96%E8%AF%91%E5%8E%9F%E7%90%86/" rel="tag"># 编译原理</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2022/01/09/%E7%AE%97%E6%B3%95%E7%BB%83%E4%B9%A0%E9%A2%981----hdu1267%E2%80%9C%E5%A3%AB%E5%85%B5%E9%98%9F%E5%88%97%E8%AE%AD%E7%BB%83%E9%97%AE%E9%A2%98%E2%80%9D/" rel="prev" title="算法练习题1----hdu1267“士兵队列训练问题”">
      <i class="fa fa-chevron-left"></i> 算法练习题1----hdu1267“士兵队列训练问题”
    </a></div>
      <div class="post-nav-item">
    <a href="/2022/01/12/%E7%AE%97%E6%B3%95%E7%BB%83%E4%B9%A0%E9%A2%982----hdu2648%20%E2%80%9C%E8%B4%AD%E7%89%A9%E2%80%9D/" rel="next" title="算法练习题2----hdu2648“购物”">
      算法练习题2----hdu2648“购物” <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%89%8D%E8%A8%80"><span class="nav-number">1.</span> <span class="nav-text">前言</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#1-1-%E5%AE%9E%E9%AA%8C%E7%9B%AE%E7%9A%84"><span class="nav-number">2.</span> <span class="nav-text">1.1 实验目的</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#1-2-%E5%AE%9E%E9%AA%8C%E4%BB%BB%E5%8A%A1"><span class="nav-number">3.</span> <span class="nav-text">1.2 实验任务</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#1-3-%E5%AE%9E%E9%AA%8C%E5%86%85%E5%AE%B9"><span class="nav-number">4.</span> <span class="nav-text">1.3 实验内容</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-3-1-%E5%AE%9E%E9%AA%8C%E8%A6%81%E6%B1%82"><span class="nav-number">4.1.</span> <span class="nav-text">1.3.1 实验要求</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#1-3-2-%E8%BE%93%E5%85%A5%E6%A0%BC%E5%BC%8F"><span class="nav-number">4.2.</span> <span class="nav-text">1.3.2 输入格式</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#1-3-3-%E8%BE%93%E5%87%BA%E6%A0%BC%E5%BC%8F"><span class="nav-number">4.3.</span> <span class="nav-text">1.3.3 输出格式</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#1-3-4-%E6%A0%B7%E4%BE%8B"><span class="nav-number">4.4.</span> <span class="nav-text">1.3.4 样例</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#1-3-5-C%E2%80%93%E8%AF%AD%E8%A8%80%E6%96%87%E6%B3%95"><span class="nav-number">4.5.</span> <span class="nav-text">1.3.5 C–语言文法</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#1-4-%E7%A8%8B%E5%BA%8F%E4%BB%A3%E7%A0%81"><span class="nav-number">5.</span> <span class="nav-text">1.4 程序代码</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-4-1-%E7%A8%8B%E5%BA%8F%E6%B5%81%E7%A8%8B%E5%9B%BE"><span class="nav-number">5.1.</span> <span class="nav-text">1.4.1 程序流程图</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#1-4-2-%E7%A8%8B%E5%BA%8F%E6%BA%90%E7%A0%81"><span class="nav-number">5.2.</span> <span class="nav-text">1.4.2 程序源码</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#1-5-%E6%80%BB%E7%BB%93"><span class="nav-number">6.</span> <span class="nav-text">1.5 总结</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="杨大熊"
      src="/images/avatar.png">
  <p class="site-author-name" itemprop="name">杨大熊</p>
  <div class="site-description" itemprop="description">各类IT知识分享</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">58</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">6</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">6</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/Yang-Da-xiong?tab=repositories" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;Yang-Da-xiong?tab&#x3D;repositories" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://blog.csdn.net/weixin_45775703?spm=1000.2115.3001.5343" title="CSDN → https:&#x2F;&#x2F;blog.csdn.net&#x2F;weixin_45775703?spm&#x3D;1000.2115.3001.5343" rel="noopener" target="_blank"><i class="fab fa-codiepie fa-fw"></i>CSDN</a>
      </span>
  </div>
  <div class="cc-license motion-element" itemprop="license">
    <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" class="cc-opacity" rel="noopener" target="_blank"><img src="/images/cc-by-nc-sa.svg" alt="Creative Commons"></a>
  </div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">杨大熊</span>
</div>

<div class="powered-by">
<i class="fa fa-user-md"></i><span id="busuanzi_container_site_uv">
  本站访客数:<span id="busuanzi_value_site_uv"></span>
</span>
</div>

<span id="busuanzi_container_site_pv">
    本站总访问量<span id="busuanzi_value_site_pv"></span>次
</span>

<div class="theme-info">
<div class="powered-by"></div>
<span class="post-count">博客全站共56.5k字</span>
</div>
        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <span class="post-meta-item" id="busuanzi_container_site_uv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item" id="busuanzi_container_site_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>




  















  

  

<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginRootPath":"live2dw/","pluginJsPath":"lib/","pluginModelPath":"assets/","tagMode":false,"model":{"jsonPath":"/live2dw/assets/wanko.model.json"},"display":{"position":"right","width":150,"height":300},"mobile":{"show":true},"log":false});</script></body>
</html>

<!-- 页面点击小红心 -->
<script type="text/javascript" src="/js/schemes/clicklove.js"></script>
